﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Windows;
using System.Windows.Media;

namespace DiskUsage
{
    public class DriveInfoPresentation : DirectoryPresentationBase
    {
        static FileSizeConverter s_fileSizeConverter = new FileSizeConverter();
        DriveInfo m_driveInfo;

        public DriveInfoPresentation(DriveInfo driveInfo)
        {
            Contract.Ensures(driveInfo != null);
            m_driveInfo = driveInfo;
        }

        public override string Name
        {
            get
            {
                if (m_driveInfo.IsReady)
                    return (string.Format("{0} ({1})", m_driveInfo.VolumeLabel, m_driveInfo.Name.Trim('\\')));
                else return m_driveInfo.Name;
            }
        }

        public string FreeSpaceLong
        {
            get
            {
                if (!m_driveInfo.IsReady) return string.Empty;

                var total = s_fileSizeConverter.Convert(m_driveInfo.TotalSize, null, null, null);
                var free = s_fileSizeConverter.Convert(m_driveInfo.TotalFreeSpace, null, null, null);
                return string.Format("{0:0.##} free of {1:#.##}", free, total);
            }

        }

        public int PercentageUsed
        {
            get
            {
                if (!m_driveInfo.IsReady) return 0;

                return (int)Math.Ceiling(
                    (m_driveInfo.TotalSize - m_driveInfo.TotalFreeSpace) / (double)m_driveInfo.TotalSize * 100);
            }
        }

        public SolidColorBrush DiskUsageBarColor
        {
            get
            {
                if (PercentageUsed <= 70) return Brushes.Green;
                return Brushes.Red;
            }
        }

        public Visibility ShowDiskUsageBar
        {
            get
            {
                return m_driveInfo.IsReady ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public override string Icon
        {
            get
            {
                switch (m_driveInfo.DriveType)
                {
                    case System.IO.DriveType.Fixed:
                        return "Resources/harddisk.png";
                    case System.IO.DriveType.CDRom:
                        return "Resources/cd.png";
                    case System.IO.DriveType.Removable:
                        return "Resources/removable.png";
                    default:
                        return string.Empty;
                }
            }
        }

        public override string SmallIcon
        {
            get
            {
                switch (m_driveInfo.DriveType)
                {
                    case System.IO.DriveType.Fixed:
                        return "Resources/harddisk_small.png";
                    case System.IO.DriveType.CDRom:
                        return "Resources/cd_small.png";
                    case System.IO.DriveType.Removable:
                        return "Resources/removable_small.png";
                    default:
                        return string.Empty;
                }
            }
        }

        public override DirectoryInfo DirectoryInfo
        {
            get { return m_driveInfo.RootDirectory; }
        }

        protected override List<DirectoryPresentationBase> PopulateChildren()
        {
            var children = new List<DirectoryPresentationBase>();
            Array.ForEach<DirectoryInfo>(m_driveInfo.RootDirectory.GetDirectories(),
                a => children.Add(new DirectoryInfoPresentation(a)));
            return children;
        }

        public override bool IsParentOf(DirectoryInfo dir)
        {
            if (dir == null || m_driveInfo == null) return false;
            return dir.FullName.StartsWith(m_driveInfo.RootDirectory.FullName);
        }
    }
}
